// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   discussion_draft_ok.java

package draft.crm;

import include.get_name_from_ID.getNameFromID;
import include.get_rate_from_ID.getRateFromID;
import include.get_three_kinds.getThreeKinds;
import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.*;

public class discussion_draft_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;
	nseer_db_backup erp_db;

	public discussion_draft_ok()
	{
		erp_db = null;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			java.io.PrintWriter out = response.getWriter();
			session = request.getSession();
			counter count = new counter(dbApplication);
			ValidataNumber validata = new ValidataNumber();
			ValidataRecord vr = new ValidataRecord();
			ValidataTag vt = new ValidataTag();
			getThreeKinds getThreeKinds = new getThreeKinds();
			getNameFromID getNameFromID = new getNameFromID();
			getRateFromID getRateFromID = new getRateFromID();
			nseer_db_backup1 crm_db = new nseer_db_backup1(dbApplication);
			if (crm_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String register_id = (String)session.getAttribute("human_IDD");
				String register = request.getParameter("register");
				String discussion_ID = request.getParameter("discussion_ID");
				String customer_ID = request.getParameter("customer_ID");
				String customer_name = request.getParameter("customer_name");
				String demand_customer_address = request.getParameter("demand_customer_address");
				String demand_customer_mailing_address = request.getParameter("demand_customer_mailing_address");
				String demand_contact_person = request.getParameter("demand_contact_person");
				String demand_contact_person_tel = request.getParameter("demand_contact_person_tel");
				String demand_contact_person_fax = request.getParameter("demand_contact_person_fax");
				String demand_pay_time = request.getParameter("demand_pay_time");
				String demand_pay_type = request.getParameter("demand_pay_type");
				String demand_pay_fee_type = request.getParameter("demand_pay_fee_type");
				String demand_gather_time = request.getParameter("demand_gather_time");
				String demand_gather_type = request.getParameter("demand_gather_type");
				String demand_gather_method = request.getParameter("demand_gather_method");
				String demand_invoice_type = request.getParameter("demand_invoice_type");
				String sales_ID = request.getParameter("sales_ID");
				String sales_name = getNameFromID.getNameFromID((String)dbSession.getAttribute("unit_db_name"), "hr_file", "human_ID", sales_ID, "human_name");
				String aaa[] = {
					"", ""
				};
				if (!sales_ID.equals(""))
					aaa = getThreeKinds.getThreeKinds((String)dbSession.getAttribute("unit_db_name"), "hr_file", "human_ID", sales_ID);
				String bodyc = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
				String remark = exchange.toHtml(bodyc);
				String modify_tag = request.getParameter("modify_tag");
				String product_amount = request.getParameter("product_amount");
				String product_IDn[] = request.getParameterValues("product_ID");
				String list_pricen[] = request.getParameterValues("list_price");
				String amountn[] = request.getParameterValues("amount");
				String off_discountn[] = request.getParameterValues("off_discount");
				int num = Integer.parseInt(product_amount);
				if (num == 0 && product_IDn.length == 1)
				{
					response.sendRedirect((new StringBuilder("draft/crm/discussion_ok_a.jsp?discussion_ID=")).append(discussion_ID).toString());
				} else
				{
					String demand_pay_fee_sum2 = request.getParameter("demand_pay_fee_sum");
					StringTokenizer tokenTO1 = new StringTokenizer(demand_pay_fee_sum2, ",");
					String demand_pay_fee_sum1;
					for (String demand_pay_fee_sum = ""; tokenTO1.hasMoreTokens(); demand_pay_fee_sum = (new StringBuilder(String.valueOf(demand_pay_fee_sum))).append(demand_pay_fee_sum1).toString())
						demand_pay_fee_sum1 = tokenTO1.nextToken();

					double order_discount = getRateFromID.getRateFromID((String)dbSession.getAttribute("unit_db_name"), "security_users", "human_ID", sales_ID, "order_discount");
					double order_discount1 = getRateFromID.getRateFromID((String)dbSession.getAttribute("unit_db_name"), "security_users", "human_ID", register_id, "order_discount");
					int n = 0;
					int m = 0;
					String product_ID_group = ",";
					for (int i = 1; i <= num; i++)
					{
						String tem_amount = (new StringBuilder("amount")).append(i).toString();
						String tem_off_discount = (new StringBuilder("off_discount")).append(i).toString();
						String tem_list_price = (new StringBuilder("list_price")).append(i).toString();
						String amount = request.getParameter(tem_amount);
						String off_discount = request.getParameter(tem_off_discount);
						String list_price2 = request.getParameter(tem_list_price);
						StringTokenizer tokenTO2 = new StringTokenizer(list_price2, ",");
						String list_price;
						String list_price1;
						for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
							list_price1 = tokenTO2.nextToken();

						if (!validata.validata(amount) || !validata.validata(off_discount) || !validata.validata(list_price))
							n++;
						else
						if (Double.parseDouble(off_discount) > order_discount && Double.parseDouble(off_discount) > order_discount1)
							n++;
					}

					for (int j = 1; j < product_IDn.length; j++)
					{
						product_ID_group = (new StringBuilder(String.valueOf(product_ID_group))).append(product_IDn[j]).append(",").toString();
						StringTokenizer tokenTO2 = new StringTokenizer(list_pricen[j], ",");
						String list_price;
						String list_price1;
						for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
							list_price1 = tokenTO2.nextToken();

						if (!validata.validata(amountn[j]) || !validata.validata(off_discountn[j]) || !validata.validata(list_price))
							n++;
						else
						if (Double.parseDouble(off_discountn[j]) > order_discount && Double.parseDouble(off_discountn[j]) > order_discount1)
							n++;
					}

					for (int i = 1; i <= num; i++)
					{
						String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
						String product_ID = request.getParameter(tem_product_ID);
						if (product_ID_group.indexOf(product_ID) != -1)
							m++;
					}

					if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "crm_discussion", "discussion_ID", discussion_ID, "check_tag").equals("5") || vt.validata((String)dbSession.getAttribute("unit_db_name"), "crm_discussion", "discussion_ID", discussion_ID, "check_tag").equals("9"))
					{
						if (n == 0)
						{
							if (m == 0)
							{
								String time = "";
								Date now = new Date();
								SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
								time = formatter.format(now);
								String id = "";
								String sql = (new StringBuilder("select id from crm_discussion where discussion_ID='")).append(discussion_ID).append("'").toString();
								ResultSet rs = crm_db.executeQuery(sql);
								if (rs.next())
									id = rs.getString("id");
								int service_count = 0;
								int stock_number = 0;
								int pay_amount_sum = 0;
								double sale_price_sum = 0.0D;
								double cost_price_sum = 0.0D;
								double real_cost_price_sum = 0.0D;
								for (int i = 1; i <= num; i++)
								{
									String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
									String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
									String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
									String tem_amount = (new StringBuilder("amount")).append(i).toString();
									String tem_off_discount = (new StringBuilder("off_discount")).append(i).toString();
									String tem_list_price = (new StringBuilder("list_price")).append(i).toString();
									String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
									String tem_real_cost_price = (new StringBuilder("real_cost_price")).append(i).toString();
									String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
									String product_name = request.getParameter(tem_product_name);
									String product_ID = request.getParameter(tem_product_ID);
									String product_describe = request.getParameter(tem_product_describe);
									String amount1 = request.getParameter(tem_amount);
									String off_discount = request.getParameter(tem_off_discount);
									String list_price2 = request.getParameter(tem_list_price);
									StringTokenizer tokenTO2 = new StringTokenizer(list_price2, ",");
									String list_price;
									String list_price1;
									for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
										list_price1 = tokenTO2.nextToken();

									String cost_price2 = request.getParameter(tem_cost_price);
									StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
									String cost_price;
									String cost_price1;
									for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
										cost_price1 = tokenTO3.nextToken();

									String real_cost_price2 = request.getParameter(tem_real_cost_price);
									StringTokenizer tokenTO4 = new StringTokenizer(real_cost_price2, ",");
									String real_cost_price;
									String real_cost_price1;
									for (real_cost_price = ""; tokenTO4.hasMoreTokens(); real_cost_price = (new StringBuilder(String.valueOf(real_cost_price))).append(real_cost_price1).toString())
										real_cost_price1 = tokenTO4.nextToken();

									String amount_unit = request.getParameter(tem_amount_unit);
									double amount = 0.0D;
									double subtotal = Double.parseDouble(list_price) * (1.0D - Double.parseDouble(off_discount) / 100D) * Double.parseDouble(amount1);
									double cost_price_after_discount_sum = Double.parseDouble(cost_price) * Double.parseDouble(amount1);
									double real_cost_price_after_discount_sum = Double.parseDouble(real_cost_price) * Double.parseDouble(amount1);
									sale_price_sum += subtotal;
									cost_price_sum += cost_price_after_discount_sum;
									real_cost_price_sum += real_cost_price_after_discount_sum;
									double order_sale_bonus_subtotal = (getRateFromID.getRateFromID((String)dbSession.getAttribute("unit_db_name"), "design_file", "product_ID", product_ID, "order_sale_bonus_rate") * subtotal) / 100D;
									double order_profit_bonus_subtotal = 0.0D;
									String sql1 = (new StringBuilder("update crm_discussion_details set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',product_describe='").append(product_describe).append("',list_price='").append(list_price).append("',amount='").append(amount1).append("',cost_price='").append(cost_price).append("',real_cost_price='").append(real_cost_price).append("',off_discount='").append(off_discount).append("',subtotal='").append(subtotal).append("' where discussion_ID='").append(discussion_ID).append("' and details_number='").append(i).append("'").toString();
									crm_db.executeUpdate(sql1);
								}

								String cost_pricen[] = request.getParameterValues("cost_price");
								String real_cost_pricen[] = request.getParameterValues("real_cost_price");
								String product_namen[] = request.getParameterValues("product_name");
								String product_describen[] = request.getParameterValues("product_describe");
								String amount_unitn[] = request.getParameterValues("amount_unit");
								for (int i = 1; i < product_IDn.length; i++)
								{
									num++;
									String list_price = list_pricen[i].replaceAll(",", "");
									String cost_price = cost_pricen[i].replaceAll(",", "");
									String real_cost_price = real_cost_pricen[i].replaceAll(",", "");
									double amount = 0.0D;
									double subtotal = Double.parseDouble(list_price) * (1.0D - Double.parseDouble(off_discountn[i]) / 100D) * Double.parseDouble(amountn[i]);
									double cost_price_after_discount_sum = Double.parseDouble(cost_price) * Double.parseDouble(amountn[i]);
									double real_cost_price_after_discount_sum = Double.parseDouble(real_cost_price) * Double.parseDouble(amountn[i]);
									sale_price_sum += subtotal;
									cost_price_sum += cost_price_after_discount_sum;
									real_cost_price_sum += real_cost_price_after_discount_sum;
									String sql1 = (new StringBuilder("insert into crm_discussion_details(discussion_ID,details_number,product_ID,product_name,product_describe,list_price,amount,cost_price,real_cost_price,off_discount,subtotal,amount_unit) values ('")).append(discussion_ID).append("','").append(num).append("','").append(product_IDn[i]).append("','").append(product_namen[i]).append("','").append(product_describen[i]).append("','").append(list_price).append("','").append(amountn[i]).append("','").append(cost_price).append("','").append(real_cost_price).append("','").append(off_discountn[i]).append("','").append(subtotal).append("','").append(amount_unitn[i]).append("')").toString();
									crm_db.executeUpdate(sql1);
								}

								sql = (new StringBuilder("update crm_discussion set customer_ID='")).append(customer_ID).append("',customer_name='").append(customer_name).append("',demand_customer_address='").append(demand_customer_address).append("',demand_customer_mailing_address='").append(demand_customer_mailing_address).append("',demand_contact_person='").append(demand_contact_person).append("',demand_contact_person_tel='").append(demand_contact_person_tel).append("',demand_contact_person_fax='").append(demand_contact_person_fax).append("',demand_pay_time='").append(demand_pay_time).append("',demand_pay_type='").append(demand_pay_type).append("',demand_pay_fee_type='").append(demand_pay_fee_type).append("',demand_gather_type='").append(demand_gather_type).append("',demand_gather_method='").append(demand_gather_method).append("',demand_invoice_type='").append(demand_invoice_type).append("',sales_name='").append(sales_name).append("',sales_ID='").append(sales_ID).append("',register='").append(register).append("',remark='").append(remark).append("',sale_price_sum='").append(sale_price_sum).append("',cost_price_sum='").append(cost_price_sum).append("',real_cost_price_sum='").append(real_cost_price_sum).append("' where discussion_ID='").append(discussion_ID).append("'").toString();
								crm_db.executeUpdate(sql);
								response.sendRedirect((new StringBuilder("draft/crm/discussion_choose_attachment.jsp?discussion_ID=")).append(discussion_ID).toString());
							} else
							{
								response.sendRedirect("draft/crm/discussion_ok.jsp?finished_tag=5");
							}
						} else
						{
							response.sendRedirect("draft/crm/discussion_ok.jsp?finished_tag=1");
						}
					} else
					{
						response.sendRedirect("draft/crm/discussion_ok.jsp?finished_tag=4");
					}
				}
				crm_db.commit();
				crm_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
